<template>
    <div class="app-container">
        <el-dialog v-model="props.visible" title="封面生产单选择" width="71%" @close="handleCancel">
            <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="auto" v-show="showSearch">
                <el-row>
                    <el-form-item label="封面生产单编号" prop="produceNo">
                        <el-input v-model.trim="queryParams.produceNo" clearable style="width: 150px;"
                                  placeholder="请输入封面生产单编号" @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-row>
            </el-form>
            <div>
<!--                <el-button style="margin-bottom: 5px;" type="primary" plain @click="handleSubmit"-->
<!--                           :disabled="multiple">确认</el-button>-->
                <el-table ref="dataListRef" v-loading="loading" :data="dataList" stripe border style="cursor: pointer;"
                        @cell-click="handleCellClick"><!--  @selection-change="handleSelectionChange" -->

                    <el-table-column label="封面生产单编号" min-width="150" align="center" prop="produceNo"
                                     :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column label="下单日期" min-width="200" align="center" prop="submitDate"
                                     :show-overflow-tooltip="true">
                        <template v-slot="{ row }">
                            {{ row.submitDate?.split('T')[0] ?? '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="交货日期" min-width="150" align="center" prop="deliverDate"
                                     :show-overflow-tooltip="true">
                        <template v-slot="{ row }">
                            {{ row.deliverDate?.split('T')[0] ?? '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="文件日期" min-width="120" align="center" prop="fileDate"
                                     :show-overflow-tooltip="true" >
                        <template v-slot="{ row }">
                            {{ row.fileDate?.split('T')[0] ?? '' }}
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                            v-model:limit="queryParams.pageSize" @pagination="getList" />
            </div>
        </el-dialog>
    </div>
</template>


<script setup name="searchMaterialReceipt">
    import { getProduceCovingByOrderId } from '@/api/production/cover'
    const { proxy } = getCurrentInstance();
    const emit = defineEmits(['changeUpdate'])
    const { mes_material_unit, } = proxy.useDict('mes_material_unit',);
    const loading = ref(true);
    const dataList = ref([])
    const selectRow = ref([])
    const multiple = ref(true)
    const showSearch = ref(true);
    const router = useRouter()
    const dataListRef =ref(null)
    const total = ref(0);
    const props = defineProps({
        visible: {
            type: Boolean,
            default: false
        },
        productCovingSelectList: {
            type: Array,
            default: () => { [] }
        },
        params:{
            type: Object,
            default: () => { { } }
        }

    })
    watch(() => props.visible, (val) => {
        if (val) {
            handleQuery()
            nextTick(() => {
                dataListRef.value.clearSelection()
                if (props.productCovingSelectList.length > 0) {
                    props.productCovingSelectList.forEach(item => {
                        dataListRef.value.toggleRowSelection(item, true)
                    })
                }
            })
        }
    })
    const data = reactive({
        queryParams: {
            pageNum: 1,
            pageSize: 10
        },
    });


    const { queryParams } = toRefs(data);

    const getList = () => {
        loading.value = true
        getProduceCovingByOrderId({...queryParams.value,...props.params}).then(res => {
            loading.value = false
            dataList.value = res.rows
            total.value = res.total
        }).catch(err => {
            loading.value = false
        })
    }
    const handleCancel = () => {
        proxy.resetForm("queryRef");
        emit('cancel')
    }
     //单选
    const handleCellClick = (row) => {
        proxy.$modal.confirm('是否确认选择「' + row.produceNo + '」').then(function () {
            emit('changeUpdate', row)
        }).catch(() => { });
    }

    // 选中
    // const handleSelectionChange = (row) => {
    //     console.log(row, 'row');
    //     selectRow.value = row
    //     multiple.value = !row.length
    // }
    // const handleSubmit = () => {
    //     emit('changeUpdate', selectRow.value)
    // }

    // 搜索
    const handleQuery = () => {
        queryParams.value.pageNum = 1;
        getList();
    }
    /** 重置按钮操作 */
    const resetQuery = () => {
        proxy.resetForm("queryRef");
        handleQuery();
    };
</script>
<style lang="scss" scoped></style>